// pages/index/oneRange/oneRange.js
import {
    GetLkyfd, // 理科一分一段列表
    GetWkyfd, // 文科一分一段列表
    GetLkFdbyscore, // 根据分数获取理科位次
    GetWkFdbyscore, // 根据分数获取文科位次
    GetStudentgrades, // 我的成绩查询
    GetFdyearlist, // 获取一分一段年份
} from '../../../api/index'

Page({

  /**
   * 页面的初始数据
   */
  data: {
        listArray:[],
        screenArray: ['', '理科'],
        yearArray:[],
        typeArray:['理科','文科'],
        screenSelectIndex:0,
        show:false,
        score:'',
        model:{},
        switchStatusWkType:'',
        switchStatusLkType:'',
        pageNo:1,
        totalPages:0,
        nomore:false,
        myScore:'--',
        mySciences:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
        listArray:[],
        pageNo: 1,
        totalPages: 0,
        nomore: false,
      })
    

    this._GetFdyearlist('', true);

    // this.setData({
    //     screenArray: screenArray
    // })
    

  },

  // 输入框输入事件
  inputEvent: function(e) {
    // e.detail.value 包含输入框的当前值
    var value = e.detail.value;
    console.log(e)
    // 可以根据 field 来区分不同的输入框，这里只有一个输入框
    this.setData({
        score: value // 更新对应的data中的值
    });
  },
  // 查询按钮点击事件
  findAction() {
    this.setData({
        listArray:[],
        pageNo: 1,
        totalPages: 0,
        nomore: false,
      })
    let year = this.data.screenArray[0].slice(0, -1);
    if (this.data.screenArray[1] == '文科') {
        this._GetWkyfd("?fdYear=" + year + "&pageNum=" + this.data.pageNo + '&pageSize=10&zf=' + this.data.score,false)
    } else {
        this._GetLkyfd("?fdYear=" + year + "&pageNum=" + this.data.pageNo + '&pageSize=10&zf=' + this.data.score,false)
    }
  },

  // 筛选框点击事件
  screenSelectAction(event) {
    
    this.setData({
        screenSelectIndex:event.currentTarget.dataset.index,
        show:true
    })
  },

  // 取消picker
  onCancel() {
    this.setData({
        show:false
    })
  },

  // 确认picker
  onConfirm(event) {
    console.log(event)
    let updatedScreenArray = this.data.screenArray; // 先获取原来的 screenArray 数据
    updatedScreenArray[this.data.screenSelectIndex] = event.detail.value;

    this.setData({
        screenArray: updatedScreenArray,
        show:false
    });
    let year = this.data.screenArray[0].slice(0, -1);
    this.setData({
        listArray:[],
        pageNo: 1,
        totalPages: 0,
        nomore: false,
      })
    if (this.data.screenArray[1] == '文科') {
        this._GetWkyfd("?fdYear=" + year + "&pageNum=" + this.data.pageNo + '&pageSize=10&zf=' + this.data.score,false)
    } else {
        this._GetLkyfd("?fdYear=" + year + "&pageNum=" + this.data.pageNo + '&pageSize=10&zf=' + this.data.score,false)
    }
  },



  // 理科一分一段列表
  _GetLkyfd(data, isLoading) {
    let that = this
    if (this.data.totalPages <= this.data.listArray.length && this.data.totalPages != 0) {
        this.setData({
          nomore: true
        })
        return;
      }
    GetLkyfd(data, isLoading).then(res => {
        console.log(res)
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            let array = res.rows;
            setTimeout(function() {
                //要实现延迟执行效果的代码快
                that.setData({
                    listArray: that.data.listArray.concat(array),
                    pageNo: that.data.pageNo + 1,
                    totalPages: res.total,
                  });
                }, 200);//设置延迟效果的具体时间
     
                  if (that.data.totalPages <= that.data.listArray.length && that.data.totalPages != 0) {
                        that.setData({
                            nomore: true
                        })
                  } 
        } else {
            Toast(res.msg)
        }
    })
},

  // 文科一分一段列表
  _GetWkyfd(data, isLoading) {
    let that = this
    if (this.data.totalPages <= this.data.listArray.length && this.data.totalPages != 0) {
        this.setData({
          nomore: true
        })
        return;
      }
    GetWkyfd(data, isLoading).then(res => {
        console.log(res)
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            let array = res.rows;
            setTimeout(function() {
            //要实现延迟执行效果的代码快
            that.setData({
                listArray: that.data.listArray.concat(array),
                pageNo: that.data.pageNo + 1,
                totalPages: res.total,
              });
            }, 100);//设置延迟效果的具体时间
            
                  if (that.data.totalPages <= that.data.listArray.length && that.data.totalPages != 0) {
                        that.setData({
                            nomore: true
                        })
                  } 
        } else {
            Toast(res.msg)
        }
    })
},

  // 根据分数获取理科位次
  _GetLkFdbyscore(data, isLoading) {
    GetLkFdbyscore(data, isLoading).then(res => {
        console.log(res)
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            this.setData({
                model:res.data
            })
        } else {
            this.setData({
                model:{}
            })
            Toast(res.msg)
        }
    })
},

 // 获取一分一段年份
 _GetFdyearlist(data, isLoading) {
    GetFdyearlist(data, isLoading).then(res => {
        console.log(res)
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            let array = res.rows
            let yaer = []
            array.forEach(item=>{
                yaer.push(item.fdYear + '年')
            })
            this.setData({
                'screenArray[0]': yaer[0],
                yearArray:yaer
            })
            let openid = wx.getStorageSync('openId') || '';
            this._GetStudentgrades('?openId=' + openid, false)
            this._GetLkyfd("?fdYear=" + this.data.yearArray[0].slice(0, -1) + "&pageNum=" + this.data.pageNo + '&pageSize=10&zf=' + this.data.score, true)
        } else {
            
        }
    })
},

  // 根据分数获取文科位次
  _GetWkFdbyscore(data, isLoading) {
    GetWkFdbyscore(data, isLoading).then(res => {
        console.log(res)
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            this.setData({
                model:res.data
            })
        } else {
            this.setData({
                model:{}
            })
            Toast(res.msg)
        }
    })
},

// 我的成绩查询
_GetStudentgrades(data, isLoading) {
    GetStudentgrades(data, isLoading).then(res => {
        console.log(res)
        let code = res.code;
        // Toast('成功')
        if (code == 200) {
            let array = res.rows;
            if (array.length != 0) {
                let model = array[0]
                let year = this.data.screenArray[0].slice(0, -1);
                this.setData({
                    myScore:model.myScore
                })
                if (model.isWuli == 1) { // 物理  理科

                    this.setData({
                        mySciences:'理科'
                    })
                    this._GetLkFdbyscore({zf:model.myScore,fdYear:year}, false)
                } else {
                    this.setData({
                        mySciences:'文科'
                    })
                    this._GetWkFdbyscore({zf:model.myScore,fdYear:year}, false)
                }
            }
        } else {
            // Toast(res.msg)
        }
    })
},


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    console.log('下拉刷新')
    // 显示顶部刷新图标
    wx.showNavigationBarLoading();
    this.setData({
        listArray:[],
        pageNo: 1,
        totalPages: 0,
        nomore: false,
      })
      let year = this.data.screenArray[0].slice(0, -1);
    if (this.data.screenArray[1] == '文科') {
        this._GetWkyfd("?fdYear=" + year + "&pageNum=" + this.data.pageNo + '&pageSize=10&zf=' + this.data.score,false)
    } else {
        this._GetLkyfd("?fdYear=" + year + "&pageNum=" + this.data.pageNo + '&pageSize=10&zf=' + this.data.score,false)
    }

    // 隐藏导航栏加载框
    wx.hideNavigationBarLoading();
    // 停止下拉动作
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    console.log('上拉加载更多')
    let year = this.data.screenArray[0].slice(0, -1);
    if (this.data.screenArray[1] == '文科') {
        this._GetWkyfd("?fdYear=" + year + "&pageNum=" + this.data.pageNo + '&pageSize=10&zf=' + this.data.score,false)
    } else {
        this._GetLkyfd("?fdYear=" + year + "&pageNum=" + this.data.pageNo + '&pageSize=10&zf=' + this.data.score,false)
    }
    console.log('上拉加载更多')
  },

  onShareTimeline: function (){

},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})